package indi.xliuovo.coursedesign.musicplayer;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.fragment.app.Fragment;

public class MineFragment extends Fragment implements View.OnClickListener {

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    private String mParam1;
    private String mParam2;

    private String tip = "正在努力开发，敬请期待...";

    private View mineFragment;
    private TextView theName, theEmail;
    private ImageButton toModify;
    private ImageView setMsg, setDownload, checkUpdate, about, share, help;

    Bundle bundle;

    public MineFragment() {
    }

    public static MineFragment newInstance(String param1, String param2) {
        MineFragment fragment = new MineFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mineFragment = inflater.inflate(R.layout.fragment_mine, container, false);
        theName = mineFragment.findViewById(R.id.name);
        theEmail = mineFragment.findViewById(R.id.email);
        toModify = mineFragment.findViewById(R.id.toModify);
        toModify.setOnClickListener(this);
        setMsg = mineFragment.findViewById(R.id.toSetMsg);
        setMsg.setOnClickListener(this);
        setDownload = mineFragment.findViewById(R.id.toSetDownload);
        setDownload.setOnClickListener(this);
        checkUpdate = mineFragment.findViewById(R.id.toCheckUpdate);
        checkUpdate.setOnClickListener(this);
        about = mineFragment.findViewById(R.id.about);
        about.setOnClickListener(this);
        share = mineFragment.findViewById(R.id.toShare);
        share.setOnClickListener(this);
        help = mineFragment.findViewById(R.id.help);
        help.setOnClickListener(this);

        return mineFragment;
    }

    @Override
    public void onResume() {
        super.onResume();
        bundle = this.getArguments();
        System.out.println(bundle.getString("userName"));
        theName.setText(bundle.getString("userName"));
        if (bundle.getString("userEmail").equals("None"))
            theEmail.setText("未设置邮箱");
        else
            theEmail.setText(bundle.getString("userEmail"));
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.toModify:
                Intent intent = new Intent(getActivity(), ModifyActivity.class);
                intent.putExtras(bundle);
                startActivity(intent);
                break;
            case R.id.about:
                startActivity(new Intent(getActivity(), AboutActivity.class));;
                break;
            case R.id.toSetMsg:
            case R.id.toSetDownload:
            case R.id.toCheckUpdate:
            case R.id.toShare:
            case R.id.help:
                Toast.makeText(getContext(), tip, Toast.LENGTH_SHORT).show();
                break;
        }
    }
}